package com.market.cash.utils.dialog

import android.content.Context
import android.graphics.Color
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.market.cash.CashRootApps
import com.market.cash.R
import com.market.cash.databinding.CashLanguageDialogBinding
import com.market.cash.pro.ProDialog
import com.market.cash.utils.event.CashRootEventHelper
import com.market.cash.utils.event.CashRootEventType
import com.market.cash.utils.helper.SharedHelper
import com.market.cash.utils.language.LocaleContract
import com.market.cash.utils.language.MultiLanguages

/**
 * CashRoot
 * @Author wwdeng
 * @CreateTime: 2024/10/29 17:59
 * @Annotation：语言选择 dialog
 */
class CashRootLanguageDialog : ProDialog {
    private lateinit var binding: CashLanguageDialogBinding
    private var mCallback: IDialogCallback? = null
    //事件备注
    private var eventRemark = ""

    var isSelect = false
    //选择语言类型
    private var selectIndex = 1

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    constructor(context: Context, remark: String,callback: IDialogCallback?) : super(context) {
        mCallback = callback
        eventRemark = remark
    }

    override fun initWindow() {
        val params = window!!.attributes
        params.width = WindowManager.LayoutParams.MATCH_PARENT
        params.height = WindowManager.LayoutParams.WRAP_CONTENT
        window!!.attributes = params
        window!!.setWindowAnimations(R.style.dialog_slide_anim) //修改动画样式
        window!!.setGravity(Gravity.BOTTOM)
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback) {
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = CashLanguageDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        isSelect = false
        checkSelectStatus()

        //当前语言
        val curtLocale = MultiLanguages.getAppLanguage(context)
        when(curtLocale.language){
            LocaleContract.getSwahiliLocale().language -> {//坦桑
                selectIndex = 2
            }
            else -> {
                selectIndex = 1
            }
        }
        checkSelectLanguageStatusViews()
    }

    override fun initListener() {
        setOnClickListener(binding.nextLayout)//确认

        binding.selectLayout.setOnClickListener {
            isSelect = !isSelect
            checkSelectStatus()
        }

        binding.englishLayout.setOnClickListener {
            CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_LANGUAGE_ENGLISH, eventRemark)
            selectIndex = 1
            checkSelectLanguageStatusViews()
        }

        binding.swahiliLayout.setOnClickListener {
            CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_LANGUAGE_TANZANIA, eventRemark)
            selectIndex = 2
            checkSelectLanguageStatusViews()
        }
    }

    override fun initData() {

    }

    override fun onViewClick(v: View?) {
        dismiss()
        if (v!!.id == R.id.nextLayout) {
            SharedHelper.saveCashRootSPData(SharedHelper.CashRoot_SPKEY.CASHROOT_LANGUAGE_LAUNCH,true)
            CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.CLICK.CASHROOT_CLICK_LANGUAGE_SUBMIT, eventRemark)
            when (selectIndex) {
                1 -> {
                    showCashRootEnglishClick()
                }
                else -> {
                    showCashRootSwahiliClick()
                }
            }
        }
    }

    /**
     * 选择状态
     */
    private fun checkSelectStatus(){
        when(isSelect){
            true -> {
                binding.selectLayout.setBackgroundResource(R.drawable.cash_language_selected_bg)
                binding.imgSelect.setImageResource(R.mipmap.cash_up_black)
                binding.languageLayout.visibility = View.VISIBLE
            }
            else -> {
                binding.selectLayout.setBackgroundResource(R.drawable.cash_radius_frame_dfe1e8_12)
                binding.imgSelect.setImageResource(R.mipmap.cash_down_black)
                binding.languageLayout.visibility = View.INVISIBLE
            }
        }
    }

    /**
     * 切换选择状态
     */
    private fun checkSelectLanguageStatusViews() {
        binding.imgEnglish.setImageResource(R.mipmap.cash_checkbox_normal)
        binding.textEnglish.setTextColor(Color.parseColor("#000000"))
        binding.englishLayout.setBackgroundColor(Color.parseColor("#00000000"))
        binding.imgSwahili.setImageResource(R.mipmap.cash_checkbox_normal)
        binding.textSwahili.setTextColor(Color.parseColor("#000000"))
        binding.swahiliLayout.setBackgroundColor(Color.parseColor("#00000000"))
        when (selectIndex) {
            1 -> {
                binding.imgEnglish.setImageResource(R.mipmap.cash_checkbox_select)
                binding.textEnglish.setTextColor(Color.parseColor("#435CFA"))
                binding.englishLayout.setBackgroundResource(R.drawable.cash_radius_fill_eceffe_top12)
                binding.textLanguage.text = CashRootApps.app.getString(R.string.language_english)
            }
            else -> {
                binding.imgSwahili.setImageResource(R.mipmap.cash_checkbox_select)
                binding.textSwahili.setTextColor(Color.parseColor("#435CFA"))
                binding.swahiliLayout.setBackgroundResource(R.drawable.cash_radius_fill_eceffe_bottom12)
                binding.textLanguage.text = CashRootApps.app.getString(R.string.language_tanzania)
            }
        }
    }

    private fun showCashRootEnglishClick() {
        if (mCallback != null) {
            mCallback!!.settingLanguageEnglish()
        }
    }

    private fun showCashRootSwahiliClick() {
        if (mCallback != null) {
            mCallback!!.settingLanguageSwahili()
        }
    }

    interface IDialogCallback {
        fun settingLanguageEnglish()

        fun settingLanguageSwahili()
    }

    override fun onStart() {
        super.onStart()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_LANGUAGE_SHOW, eventRemark)
    }

    override fun dismiss() {
        super.dismiss()
        CashRootEventHelper.addCashRootRequestEventAction(CashRootEventType.DIALOG.CASHROOT_DIALOG_LANGUAGE_DISMISS, eventRemark)
    }
}